/*=============================================================================
# FileName    :	axi_udp_top.v
# Author      :	author
# Email       :	email@email.com
# Description :	
# Version     :	1.0
# LastChange  :	2019-07-24 11:01:34
# ChangeLog   :	
=============================================================================*/

`timescale 1ns / 1ps

module axi_udp_top #
(
    parameter C_CLK_FREQ_HZ     = 125_000_000,
    parameter LOCAL_MAC_ADDR    = 48'hC400C4010102,
    parameter LOCAL_IP_ADDR     = {8'd192,8'd168,8'd1,8'd102},
    parameter LOCAL_UDP_PORT    = 16'd4444,
    parameter REMOTE_UDP_PORT   = 16'd6666
)
(
    input                       rst,                                                
    input                       clk,

    // MAC rx  上游模块数据输入
    output                      mac_s_axis_tready,
    input   [07:00]             mac_s_axis_tdata,
    input                       mac_s_axis_tvalid,
    input                       mac_s_axis_tlast,


    output  [47:00]             udp_m_axis_tsrcmac,
    output  [31:00]             udp_m_axis_tsrcip,

    output  [07:00]             udp_m_axis_tdata,
    output                      udp_m_axis_tvalid,
    output                      udp_m_axis_tlast,

    input   [47:00]             udp_s_axis_tdstmac,
    input   [31:00]             udp_s_axis_tdstip,
    input   [10:00]             udp_s_axis_tlen,

    input   [07:00]             udp_s_axis_tdata,
    input                       udp_s_axis_tvalid,
    output                      udp_s_axis_tready,
    input                       udp_s_axis_tlast,

    // MAC tx                                     
    output  [07:00]             mac_m_axis_tdata,
    output                      mac_m_axis_tvalid,
    input                       mac_m_axis_tready,
    output                      mac_m_axis_tlast
);

wire    [47:00]             rx_eth_ii_src_mac_addr;
wire    [31:00]             rx_ipv4_src_ip_addr;
wire    [15:00]             rx_ipv4_total_len;
                                           
wire                        rx_arp_req_valid;
wire    [47:00]             rx_arp_sender_mac_addr;
wire    [31:00]             rx_arp_sender_ip_addr;
                                           
wire                        rx_icmp_req_valid;
wire    [10:00]             rx_icmp_req_len;
wire                        rx_icmp_req_wren;  
wire    [07:00]             rx_icmp_req_data;
wire    [10:00]             rx_icmp_req_addr;

// ARP 输出的数据
wire    [07:00]             tx0_axis_fifo_tdata;
wire                        tx0_axis_fifo_tvalid;
wire                        tx0_axis_fifo_tlast;
wire                        tx0_axis_fifo_tready;
wire                        tx0_axis_fifo_tfirst;
                                        
// PING输出的数据
wire    [07:00]             tx1_axis_fifo_tdata;
wire                        tx1_axis_fifo_tvalid;
wire                        tx1_axis_fifo_tlast;
wire                        tx1_axis_fifo_tready;
wire                        tx1_axis_fifo_tfirst;
                                        
// 用户通过UDP协议发送的数据
wire    [07:00]             tx2_axis_fifo_tdata;
wire                        tx2_axis_fifo_tvalid;
wire                        tx2_axis_fifo_tlast;
wire                        tx2_axis_fifo_tready;
wire                        tx2_axis_fifo_tfirst;
                                        
//------------------------------------------------
//----------------- ARP
//------------------------------------------------
arp_reply #
(
    .LOCAL_MAC_ADDR            (    LOCAL_MAC_ADDR            ),
    .LOCAL_IP_ADDR             (    LOCAL_IP_ADDR             )
)
arp_reply_i
(
    .clk                       (    clk                       ),
    .rst                       (    rst                       ),

    .rx_arp_req_valid          (    rx_arp_req_valid          ),
    .rx_arp_sender_mac_addr    (    rx_arp_sender_mac_addr    ),
    .rx_arp_sender_ip_addr     (    rx_arp_sender_ip_addr     ),

    .m_axis_tdata              (    tx0_axis_fifo_tdata       ),
    .m_axis_tvalid             (    tx0_axis_fifo_tvalid      ),
    .m_axis_tready             (    tx0_axis_fifo_tready      ),
    .m_axis_tlast              (    tx0_axis_fifo_tlast       )
);

//------------------------------------------------
//----------------- PING
//------------------------------------------------
icmp_reply #
(
    .LOCAL_MAC_ADDR            (    LOCAL_MAC_ADDR            ),
    .LOCAL_IP_ADDR             (    LOCAL_IP_ADDR             )
)
icmp_reply_i
(
    .clk                       (    clk                       ),
    .rst                       (    rst                       ),

    .rx_eth_ii_src_mac_addr    (    rx_eth_ii_src_mac_addr    ),
    .rx_ipv4_src_ip_addr       (    rx_ipv4_src_ip_addr       ),
    .rx_ipv4_total_len         (    rx_ipv4_total_len         ),

    .rx_icmp_req_valid         (    rx_icmp_req_valid         ),
    .rx_icmp_req_len           (    rx_icmp_req_len           ),
    .rx_icmp_req_wren          (    rx_icmp_req_wren          ),
    .rx_icmp_req_data          (    rx_icmp_req_data          ),
    .rx_icmp_req_addr          (    rx_icmp_req_addr          ),

    .m_axis_tdata              (    tx1_axis_fifo_tdata       ),
    .m_axis_tvalid             (    tx1_axis_fifo_tvalid      ),
    .m_axis_tready             (    tx1_axis_fifo_tready      ),
    .m_axis_tlast              (    tx1_axis_fifo_tlast       )
);

//------------------------------------------------
//----------------- UDP
//------------------------------------------------
axi_mac_tx #
(
    .LOCAL_MAC_ADDR            (    LOCAL_MAC_ADDR            ),
    .LOCAL_IP_ADDR             (    LOCAL_IP_ADDR             ),
    .LOCAL_UDP_PORT            (    LOCAL_UDP_PORT            ),
    .REMOTE_UDP_PORT           (    REMOTE_UDP_PORT           )
)
axi_mac_tx_i
(
    .clk                       (    clk                       ),
    .rst                       (    rst                       ),

    .s_axis_tdstmac            (    udp_s_axis_tdstmac        ),
    .s_axis_tdstip             (    udp_s_axis_tdstip         ),
    .s_axis_tlen               (    udp_s_axis_tlen           ),
    .s_axis_tdata              (    udp_s_axis_tdata          ),
    .s_axis_tvalid             (    udp_s_axis_tvalid         ),
    .s_axis_tready             (    udp_s_axis_tready         ),
    .s_axis_tlast              (    udp_s_axis_tlast          ),

    .m_axis_tdata              (    tx2_axis_fifo_tdata       ),
    .m_axis_tvalid             (    tx2_axis_fifo_tvalid      ),
    .m_axis_tlast              (    tx2_axis_fifo_tlast       ),
    .m_axis_tready             (    tx2_axis_fifo_tready      )
);

//------------------------------------------------
//----------------- AXI_Tx_Arbitrator
//------------------------------------------------
axi_tx_arbiter axi_tx_arbiter_i
(
    .clk                       (    clk                       ),
    .rst                       (    rst                       ),

    .s0_axis_tdata             (    tx0_axis_fifo_tdata       ),
    .s0_axis_tvalid            (    tx0_axis_fifo_tvalid      ),
    .s0_axis_tlast             (    tx0_axis_fifo_tlast       ),
    .s0_axis_tready            (    tx0_axis_fifo_tready      ),
    .s0_axis_tfirst            (                              ),

    .s1_axis_tdata             (    tx1_axis_fifo_tdata       ),
    .s1_axis_tvalid            (    tx1_axis_fifo_tvalid      ),
    .s1_axis_tlast             (    tx1_axis_fifo_tlast       ),
    .s1_axis_tready            (    tx1_axis_fifo_tready      ),
    .s1_axis_tfirst            (                              ),

    .s2_axis_tdata             (    tx2_axis_fifo_tdata       ),
    .s2_axis_tvalid            (    tx2_axis_fifo_tvalid      ),
    .s2_axis_tlast             (    tx2_axis_fifo_tlast       ),
    .s2_axis_tready            (    tx2_axis_fifo_tready      ),
    .s2_axis_tfirst            (                              ),

    .m_axis_tdata              (    mac_m_axis_tdata          ),
    .m_axis_tvalid             (    mac_m_axis_tvalid         ),
    .m_axis_tlast              (    mac_m_axis_tlast          ),
    .m_axis_tready             (    mac_m_axis_tready         ),
    .m_axis_tfirst             (                              )
);

//------------------------------------------------
//----------------- ARP,ICMP, UDP接收模块
//------------------------------------------------
axi_mac_rx #
(
    .C_CLK_FREQ_HZ             (    C_CLK_FREQ_HZ             ),
    .LOCAL_MAC_ADDR            (    LOCAL_MAC_ADDR            ),
    .LOCAL_IP_ADDR             (    LOCAL_IP_ADDR             ),
    .LOCAL_UDP_PORT            (    LOCAL_UDP_PORT            )
)
axi_mac_rx_i
(
    .clk                       (    clk                       ),
    .rst                       (    rst                       ),

    .s_axis_tdata              (    mac_s_axis_tdata          ),
    .s_axis_tvalid             (    mac_s_axis_tvalid         ),
    .s_axis_tlast              (    mac_s_axis_tlast          ),
    .s_axis_tready             (    mac_s_axis_tready         ),

    .rx_eth_ii_src_mac_addr    (    rx_eth_ii_src_mac_addr    ),
    .rx_ipv4_src_ip_addr       (    rx_ipv4_src_ip_addr       ),
    .rx_ipv4_total_len         (    rx_ipv4_total_len         ),

    .rx_arp_req_valid          (    rx_arp_req_valid          ),
    .rx_arp_sender_mac_addr    (    rx_arp_sender_mac_addr    ),
    .rx_arp_sender_ip_addr     (    rx_arp_sender_ip_addr     ),

    .rx_icmp_req_valid         (    rx_icmp_req_valid         ),
    .rx_icmp_req_len           (    rx_icmp_req_len           ),
    .rx_icmp_req_wren          (    rx_icmp_req_wren          ),
    .rx_icmp_req_data          (    rx_icmp_req_data          ),
    .rx_icmp_req_addr          (    rx_icmp_req_addr          ),

    .udp_m_axis_tsrcmac        (    udp_m_axis_tsrcmac        ),
    .udp_m_axis_tsrcip         (    udp_m_axis_tsrcip         ),
    .udp_m_axis_tdata          (    udp_m_axis_tdata          ),
    .udp_m_axis_tvalid         (    udp_m_axis_tvalid         ),
    .udp_m_axis_tlast          (    udp_m_axis_tlast          )
);

endmodule
